-- set_main_target_weight
--
local import = import
---@type behavior_ret
local bret = import("bt/bt_plugin/behavior3/behavior_ret")

---@type MAIN_TARGET_TYPE_CONFIG
local MAIN_TARGET_TYPE_CONFIG = import("bt/custom_node/enum_type_cfg/MAIN_TARGET_TYPE_CONFIG")
local MAIN_TARGET_TYPE = MAIN_TARGET_TYPE_CONFIG.TYPE
---@type role
local role = import("game/func/role")
local M = {
    name = "set_main_target_weight",
    type = "Action",
    desc = "设置功能权重",
    args = {
        { "minLevel", "int", "最小等级" },
        { "maxLevel", "int", "最大等级" },
        {
            name = "mainTargetType",
            type = "enum",
            desc = "功能类型",
            options = MAIN_TARGET_TYPE_CONFIG.main_target_weight_options
        },
        { "weight", "float", "权重" },
        {
            name = "dungeonName",
            type = "enum?",
            desc = "功能类型",
            options = {
                { name = '繁荣之地', value = '繁荣之地' },
                { name = '伊莱塔神殿', value = '伊莱塔神殿' },
                { name = '马萨尔塔冰洞', value = '马萨尔塔冰洞' },
                { name = '圣科纳遗址', value = '圣科纳遗址' },
                { name = '冬季庆典', value = '冬季庆典' },
                { name = '修炼之林', value = '修炼之林' },
            }
        },
        {
            name = "executeTime",
            type = "int?",
            desc = "执行时间(秒)",
        },
        {
            name = "isSetRole",
            type = "boolean?",
            desc = "是否设置4角色",
        },
    },
}

function M.run(node, env, value)
    local owner = env.owner
    local mainTargetType = node.args.mainTargetType
    local minLevel = node.args.minLevel
    local maxLevel = node.args.maxLevel
    local weight = node.args.weight
    local isSetRole = node.args.isSetRole or false
    local dungeonName = node.args.dungeonName
    local executeTime = node.args.executeTime
    if not owner.main_target_weight_config then
        owner.main_target_weight_config = {}
    end
    local tmpData = nil
    for _, v in ipairs(owner.main_target_weight_config) do
        if minLevel == v.minLevel and maxLevel == v.maxLevel then
            tmpData = v
            break
        end
    end
    local isInsert = false
    if not tmpData then
        isInsert = true
        tmpData = {}
        tmpData.config = {}
    end
    tmpData.minLevel = minLevel
    tmpData.maxLevel = maxLevel
    if mainTargetType == '主线' then
        table.insert(tmpData.config, { mainTarget = MAIN_TARGET_TYPE.MAIN_TASK, randomWeight = weight, isSet = isSetRole })
        role.is_set_role(isSetRole, owner, MAIN_TARGET_TYPE.MAIN_TASK, minLevel, maxLevel, mainTargetType)
    elseif mainTargetType == '副本' then
        table.insert(tmpData.config, {
            arr = {
                { mainTarget = MAIN_TARGET_TYPE.ENTER_DUNGEON, data = { mainName = dungeonName } },
                { mainTarget = MAIN_TARGET_TYPE.DUNGEON,       data = { executeTime = executeTime, mainName = dungeonName } },
            },
            randomWeight = weight,
            isSet = isSetRole
        })
        table.insert(owner.selectDunInfo, { mainName = dungeonName, minLevel = minLevel, maxLevel = maxLevel })
        role.is_set_role(isSetRole, owner, dungeonName, minLevel, maxLevel, mainTargetType)
    elseif mainTargetType == '挂机' then
        table.insert(owner.huntInfo,
            { mainName = mainTargetType, data = { executeTime = executeTime, mainName = MAIN_TARGET_TYPE.HUNT } })
        table.insert(tmpData.config,
            { mainTarget = MAIN_TARGET_TYPE.HUNT, randomWeight = weight, data = { executeTime = executeTime, mainName = MAIN_TARGET_TYPE.HUNT }, isSet = isSetRole })
        role.is_set_role(isSetRole, owner, MAIN_TARGET_TYPE.HUNT, minLevel, maxLevel, mainTargetType)
    elseif mainTargetType == '支线' then
        table.insert(tmpData.config, { mainTarget = MAIN_TARGET_TYPE.NORMAL_TASK, randomWeight = weight, isSet = isSetRole })
        role.is_set_role(isSetRole, owner, MAIN_TARGET_TYPE.NORMAL_TASK, minLevel, maxLevel, mainTargetType)
    elseif mainTargetType == '主线2' then
        table.insert(tmpData.config, { mainTarget = MAIN_TARGET_TYPE.MAIN_TASK_2, randomWeight = weight, isSet = isSetRole })
        role.is_set_role(isSetRole, owner, MAIN_TARGET_TYPE.MAIN_TASK_2, minLevel, maxLevel, mainTargetType)
    elseif mainTargetType == '激战地挂机' then
        table.insert(owner.huntInfo,
            { mainName = mainTargetType, data = { executeTime = executeTime, mainName = MAIN_TARGET_TYPE.HUNT_BATTLEPLACE } })
        table.insert(tmpData.config,
            { mainTarget = MAIN_TARGET_TYPE.HUNT_BATTLEPLACE, randomWeight = weight, data = { executeTime = executeTime, mainName = MAIN_TARGET_TYPE.HUNT_BATTLEPLACE }, isSet = isSetRole })
        role.is_set_role(isSetRole, owner, MAIN_TARGET_TYPE.HUNT_BATTLEPLACE, minLevel, maxLevel, mainTargetType)
    elseif mainTargetType == '每日任务' then
        table.insert(tmpData.config, { mainTarget = MAIN_TARGET_TYPE.DAILY_TASK, randomWeight = weight, isSet = isSetRole })
        table.insert(owner.dailyTaskInfo, { mainName = mainTargetType })
        role.is_set_role(isSetRole, owner, MAIN_TARGET_TYPE.DAILY_TASK, minLevel, maxLevel, mainTargetType)
    end
    if isInsert then
        table.insert(owner.main_target_weight_config, tmpData)
    end

    return bret.SUCCESS
end

return M
